import random

from flask import Flask, request
from flask_cors import cross_origin

from main import Pica

app = Flask(__name__)
# 因为flask的session是通过加密之后放到了cookie中。所以有加密就有密钥用于解密，所以，
# 只要用到了flask的session模块就一定要配置“SECRET_KEY”这个全局宏。一般设置为24位的字符

a = Pica()

session = {

}


# 这里默认的是get请求方式

@app.route("/login")
@cross_origin(supports_credentials=True)
def login():
    email = request.args.get('email')
    password = request.args.get('password')
    result = a.login(email=email, password=password)
    session[request.headers['email']] = result["data"]["token"]
    return result


@app.route("/profile")
@cross_origin(supports_credentials=True)
def getProfile():
    if verifyToken(request.headers):
        return a.person_info(token=request.headers['token'])
    else:
        return uselessToken()


@app.route("/mainPage")
@cross_origin(supports_credentials=True)
def mainPage():
    if verifyToken(request.headers):
        return a.main_page(token=request.headers['token'])
    else:
        return uselessToken()


@app.route("/keyword")
@cross_origin(supports_credentials=True)
def keyword():
    if verifyToken(request.headers):
        return a.keyword(token=request.headers['token'])
    else:
        return uselessToken()


@app.route("/comics")
@cross_origin(supports_credentials=True)
def comics():
    block = request.args.get('block')
    page = request.args.get('page')
    order = request.args.get('order')
    if verifyToken(request.headers):
        return a.comics(block=block, page=page, order=order, token=request.headers['token'])
    else:
        return uselessToken()


@app.route("/comicInfo")
@cross_origin(supports_credentials=True)
def comicInfo():
    book_id = request.args.get('id')
    if verifyToken(request.headers):
        return a.comic_info(token=request.headers['token'], book_id=book_id)
    else:
        return uselessToken()


@app.route("/comicEpisodes")
@cross_origin(supports_credentials=True)
def comicEpisodes():
    book_id = request.args.get('id')
    page = request.args.get('page')
    if verifyToken(request.headers):
        return a.episodes(token=request.headers['token'], book_id=book_id, page=page)
    else:
        return uselessToken()


@app.route("/comicPicture")
@cross_origin(supports_credentials=True)
def comicPicture():
    book_id = request.args.get('book_id')
    ep_id = request.args.get('ep_id')
    page = request.args.get('page')
    if verifyToken(request.headers):
        return a.picture(token=request.headers['token'], book_id=book_id, ep_id=ep_id, page=page)
    else:
        return uselessToken()


@app.route("/myFavorite")
@cross_origin(supports_credentials=True)
def myFavorite():
    page = request.args.get('page')
    if verifyToken(request.headers):
        return a.my_favourite(token=request.headers['token'], page=page)
    else:
        return uselessToken()


@app.route("/fav")
@cross_origin(supports_credentials=True)
def fav():
    book_id = request.args.get('book_id')
    if verifyToken(request.headers):
        return a.favourite(token=request.headers['token'], book_id=book_id)
    else:
        return uselessToken()



@app.route("/like")
@cross_origin(supports_credentials=True)
def like():
    book_id = request.args.get('book_id')
    if verifyToken(request.headers):
        return a.like(token=request.headers['token'], book_id=book_id)
    else:
        return uselessToken()



@app.route("/comicComment")
@cross_origin(supports_credentials=True)
def getComment():
    page = request.args.get('page')
    book_id = request.args.get('book_id')
    if verifyToken(request.headers):
        return a.get_comment(token=request.headers['token'], book_id=book_id, page=page)
    else:
        return uselessToken()


@app.route("/recommend")
@cross_origin(supports_credentials=True)
def recommend():
    book_id = request.args.get('book_id')
    if verifyToken(request.headers):
        return a.recomm(token=request.headers['token'], book_id=book_id)
    else:
        return uselessToken()


@app.route("/comicSearch", methods=['POST'])
@cross_origin(supports_credentials=True)
def comicSearch():
    page = request.json.get('page')
    key_word = request.json.get('keyword')
    sort = request.json.get('sort')
    if verifyToken(request.headers):
        return a.search(token=request.headers['token'], keyword=key_word, page=page, sort=sort)
    else:
        return uselessToken()



# @app.route("/myComment")
# @cross_origin(supports_credentials=True)
# def comicSearch():
#     page = request.json.get('page')
#     return a.my_comment(page=page)


@app.route("/test")
@cross_origin(supports_credentials=True)
def test():
    print(request.headers['email'])
    name = request.args.get('name')
    age = request.args.get('age')
    return name + age


def verifyToken(header):
    # print(session)
    # print(header['token'])
    if session.get(header['email']) == header['token']:
        return True
    else:
        return False


def uselessToken():
    return {
        'code': '400',
        'message': 'uselessToken'
    }


# 控制端口
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8848, debug=True)
